/**
 * 
 */
package datamodel.trie;

/**
 * class for a node in the trie<br>
 * save reference to its children and parent
 * 
 * @author Jakob Karolus, Kevin Munk, Magnus Brand
 * @version 1.0
 *
 */
public class TrieNode {
	
	/**
	 * Represents the children of this TrieNode
	 */
	private TrieNode[] groups = new TrieNode[16];
	
	/**
	 * Represents the parent of this TrieNode
	 */
	private TrieNode parent = null;
	
	/**
	 * Root constructor
	 */
	public TrieNode() {
		this(null);
	}
	
	/**
	 * Constructor
	 * @param parent is the parent of this TrieNode
	 */
	public TrieNode(TrieNode parent) {
		this.parent = parent;
	}
	
	/**
	 * Sets a specified TrieNode as the child of this TrieNode corresponding to a given group
	 * @param child is the child TrieNode to be set
	 * @param group is the group to which the child belongs
	 * @return true if the setting was successful
	 */
	public boolean setChildForGroup(TrieNode child, String group) {
		if(! Trie.isValidGroup(group)){
			return false;
		}
		groups[Integer.valueOf(group, 2)] = child;
		return true;
	}
	
	/**
	 * Determines if this TrieNode has a child in the specified group
	 * @param group is the specified group
	 * @return true if thsi TrieNode has a child in the specified group
	 */
	public boolean hasChild(String group){
		if(! Trie.isValidGroup(group)){
			throw new IllegalArgumentException("Invalid group name");
		}
		return (groups[Integer.valueOf(group, 2)] != null);
	}
	
	/**
	 * Retrieves the child in a specified group
	 * @param group is the specified group
	 * @return the child in a specified group
	 */
	public TrieNode getChild(String group){
		if(!this.hasChild(group)){
			throw new IllegalArgumentException("No child at that address");
		}
		return groups[Integer.valueOf(group, 2)];
	}
	
	/**
	 * Determines if this TrieNode has a parent
	 * @return true if this TriedNode has a parent
	 */
	public boolean hasParent(){
		return (this.parent != null);
	}
	
	/**
	 * Retrieves the parent of this node
	 * @return the parent of this node
	 */
	public TrieNode getParent() {
		return parent;
	}

}
